/**
 * json转字符串工具类
 * 
 */
jQuery.extend({
	// 转换为json 字符串
	stringifyJSON : function(object) {
		if (object == null)
			object = '';
		var type = typeof object;
		if ('object' == type) {
			if (Array == object.constructor)
				type = 'array';
			else if (RegExp == object.constructor)
				type = 'regexp';
			else
				type = 'object';
		}
		switch (type) {
		case 'undefined':
		case 'unknown':
			return;
			break;
		case 'function':
		case 'boolean':
		case 'regexp':
			return object.toString();
			break;
		case 'number':
			return isFinite(object) ? object.toString() : 'null';
			break;
		case 'string':
			return '"'
					+ object.replace(/(\\|\")/g, "\\$1").replace(
							/\n|\r|\t/g,
							function() {
								var a = arguments[0];
								return (a == '\n') ? '\\n'
										: (a == '\r') ? '\\r'
												: (a == '\t') ? '\\t' : ""
							}) + '"';
			break;
		case 'object':
			if (object === null)
				return 'null';
			var results = [];
			for ( var property in object) {
				var value = jQuery.stringifyJSON(object[property]);
				if (value !== undefined)
					results.push(jQuery.stringifyJSON(property) + ':' + value);
			}
			return '{' + results.join(',') + '}';
			break;
		case 'array':
			var results = [];
			for ( var i = 0; i < object.length; i++) {
				var value = jQuery.stringifyJSON(object[i]);
				if (value !== undefined)
					results.push(value);
			}
			return '[' + results.join(',') + ']';
			break;
		}
	}
});
/**
 * 清空form中的数据
 * 
 * @param id
 */
function clearform(id) {
	// input
	$("#" + id).find("input").filter(":text").each(function(i) {
		if (this.name != undefined && this.name != "") {
			$(this).val("");
		}
	});
	// hidden
	$("#" + id).find("input").filter(":hidden").each(function(i) {
		if (this.name != undefined && this.name != "") {
			$(this).val("");
		}
	});

	// select
	$("#" + id).find("select").each(function(i) {
		$(this).val("");
	});

	// 多行文本
	$("#" + id).find("textarea").each(function(i) {
		$(this).val("");
	});

	// 单选框
	$("#" + id).find("input").filter(":radio").each(function(i) {
		$(this).attr("checked", false);
	});

	// 复选框

	$("#" + id).find("input").filter(":checkbox").each(function(i) {
		$(this).attr("checked", false); // 全不选
	});
}

function disabledAreaById(areaId) {
	// input
	$("#" + areaId).find("input").each(function(i) {
		$(this).attr("readOnly", true);
	});

	// select
	$("#" + areaId).find("select").each(function(i) {
		$(this).attr("disabled", true);
	});

	// 多行文本
	$("#" + areaId).find("textarea").each(function(i) {
		$(this).attr("readOnly", true);
	});

	// 单选框
	$("#" + areaId).find("input").filter(":radio").each(function(i) {
		$(this).attr("disabled", true);
	});

	// 复选框
	$("#" + areaId).find("input").filter(":checkbox").each(function(i) {
		$(this).attr("disabled", true);
	});
};
function disabledAreaByIdNotIncludeReadOnly(areaId) {
	// input
	$("#" + areaId).find("input").each(function(i) {
		$(this).attr("disabled", true);
	});

	// select
	$("#" + areaId).find("select").each(function(i) {
		$(this).attr("disabled", true);
	});

	// 多行文本
	$("#" + areaId).find("textarea").each(function(i) {
		$(this).attr("disabled", true);
	});

	// 单选框
	$("#" + areaId).find("input").filter(":radio").each(function(i) {
		$(this).attr("disabled", true);
	});

	// 复选框
	$("#" + areaId).find("input").filter(":checkbox").each(function(i) {
		$(this).attr("disabled", true);
	});
};

function unDisabledAreaById(areaId) {
	// input
	$("#" + areaId).find("input").each(function(i) {
		$(this).removeAttr("readOnly");
	});

	// select
	$("#" + areaId).find("select").each(function(i) {
		$(this).removeAttr("disabled");
	});

	// 多行文本
	$("#" + areaId).find("textarea").each(function(i) {
		$(this).removeAttr("readOnly");
	});

	// 单选框
	$("#" + areaId).find("input").filter(":radio").each(function(i) {
		$(this).removeAttr("disabled");
	});

	// 复选框
	$("#" + areaId).find("input").filter(":checkbox").each(function(i) {
		$(this).removeAttr("disabled");
	});
};
function unDisabledAreaByIdNotIncludeReadOnly(areaId) {
	// input
	$("#" + areaId).find("input").each(function(i) {
		$(this).removeAttr("disabled");
	});

	// select
	$("#" + areaId).find("select").each(function(i) {
		$(this).removeAttr("disabled");
	});

	// 多行文本
	$("#" + areaId).find("textarea").each(function(i) {
		$(this).removeAttr("disabled");
	});

	// 单选框
	$("#" + areaId).find("input").filter(":radio").each(function(i) {
		$(this).removeAttr("disabled");
	});

	// 复选框
	$("#" + areaId).find("input").filter(":checkbox").each(function(i) {
		$(this).removeAttr("disabled");
	});
};

/**
 * 
 * 将json对象中的值赋到form中,本方法依赖于jquery.js
 * 
 * @param json
 *            json对象
 * 
 * @param id
 *            表单ID
 * 
 * @return
 * 
 */

function json2form(jsonArr, id) {

	var json = $.extend({}, jsonArr);

	// 单行文本
	$("#" + id).find("input").filter(":text").each(function(i) {
		if (this.name != undefined && this.name != "") {
			if (json[this.name] === 0) {
				json[this.name] = '0';
			}
			$(this).val(json[this.name] ? json[this.name] : this.value);
		}
	});

	// 密码框
	$("#" + id).find("input").filter(":password").each(function(i) {
		if (json[this.name] === 0) {
			json[this.name] = '0';
		}
		$(this).val(json[this.name] ? json[this.name] : this.value);
	});

	// 隐藏框
	$("#" + id).find("input").filter(":hidden").each(function(i) {
		if ($(this).attr('type') == 'hidden') {
			if (json[this.name] === 0) {
				json[this.name] = '0';
			}
			$(this).val(json[this.name] ? json[this.name] : this.value);
		}
	});

	// 下拉
	$("#" + id).find("select").each(function(i) {
		if (json[this.name] === 0) {
			json[this.name] = '0';
		}
		$(this).val(json[this.name] ? json[this.name] : this.value);
	});

	// 多行文本
	$("#" + id).find("textarea").each(function(i) {
		if (json[this.name] === 0) {
			json[this.name] = '0';
		}
		$(this).val(json[this.name] ? json[this.name] : this.value);

	});

	// 单选框
	var radioNames = "";
	$("#" + id).find("input").filter(":radio").each(function(i) {
		radioNames += radioNames.indexOf(this.name) == -1 ? this.name + "," : "";
	});
	radioNames = radioNames == "" ? "" : radioNames.substring(0,radioNames.length - 1);
	var strArr = radioNames.split(",");
	for ( var i = 0; i < strArr.length; i++) {
		var na = strArr[i];
		var temp = json[na]===undefined ? "":json[na];
		if(temp==""){
			continue;
		}
		$("#" + id).find("input[name='" + na + "']").filter(":radio").each(function(m) {
			if (json[na] === 0) {
				json[na] = '0';
			} else if (json[na] != "") {
				$(this).attr("checked", this.value == json[na]);
			}
		});
	}

	// 复选框
	var checkNames = "";
	$("#" + id).find("input").filter(":checkbox").each(function(i) {
		checkNames += checkNames.indexOf(this.name) == -1 ? this.name + "," : "";
	});
	checkNames = checkNames == "" ? "" : checkNames.substring(0,checkNames.length - 1);
	var checkArr = checkNames.split(",");
	for ( var k = 0; k < checkArr.length; k++) {
		var na = checkArr[k];
		if (na != "") {
			var temp = json[na]===undefined ? "":json[na];
			if(temp==""||temp==undefined){
				continue;
			}
			//var values = temp.split(",");
			$("#" + id).find("input[name='" + na + "']").filter(":checkbox").each(function(m) {
				var str = temp.toString();
				if(str.indexOf(this.value)!=-1){
					this.checked = true;
				}else{
					this.checked = false;
				}
//				var checkObj = this;
//				$.each(values,function(i,n){
//					if(checkObj.value==n){
//						checkObj.checked=true; 
//						return false;
//					}else{
//						checkObj.checked=false; 
//					} 
//				})
			});
		}
	}
}

/**
 * 将form序列化成json字符串,本方法依赖于jquery.js
 * 
 * @param id
 *            区域ID
 * @param add
 *            是否添加checkBox非勾选值，true添加
 * @returns
 */
function form2json(id) {

	var str = "";

	// 单行文本
	$("#" + id).find("input").filter(":text").each(function() {
		if (this.name != null && this.name != "") {
			str += "\"" + this.name + "\":" + "\"" + trim_quote(this.value) + "\",";
		}
	});

	// 单行密码
	$("#" + id).find("input").filter(":password").each(function() {
		if (this.name != null && this.name != "") {
			str += "\"" + this.name + "\":" + "\"" + trim_quote(this.value) + "\",";
		}
	});

	// 隐藏
	$("#" + id + " input[type='hidden']").each(function() {
		if (this.name != null && this.name != "") {
			str += "\"" + this.name + "\":" + "\"" + trim_quote(this.value) + "\",";
		}
	});
	
	// 单选框
	$("#" + id).find("input").filter(":radio").filter(":checked").each(function() {
		if (this.name != null && this.name != "") {
			str += "\"" + this.name + "\":" + "\"" + trim_quote(this.value) + "\",";
		}
	});

	// 下拉列表
	$("#" + id).find("select").each(function() {
		if (this.name != null && this.name != "") {
			str += "\"" + this.name + "\":" + "\"" + trim_quote(this.value) + "\",";
		}
	});
	
	// 复选框
	var checkboxNames = "";
	$("#" + id).find("input").filter(":checkbox").each(function() {
		if (checkboxNames.indexOf(this.name) == -1) {
			checkboxNames += this.name + ",";
		}
	});
	checkboxNames = checkboxNames == "" ? "" : checkboxNames.substring(0,checkboxNames.length - 1);
	var arr = checkboxNames.split(",");
	for (var i = 0; i < arr.length; i++) {
		var na = arr[i];
		if (na != "") {
			var values = "";
			$("#" + id).find("input[name='" + na + "']").filter(":checkbox").each(function() {
				if(this.name == na){
					if(this.checked){
						values += this.value+",";
					}
				}
			});
			values = values==""?"":values.substring(0,values.length-1);
			str += "\"" + na + "\":" + "\"" + trim_quote(values) + "\",";
			/*
			 * values = values==""?"":values.substring(0,values.length-1);
			 *
			 * str+="\""+na+"\":["+values+"],";
			 */
		}
	}

	// 多行文本
	$("#" + id).find("textarea").each(function() {
		if (this.name != null && this.name != "") {
			str += "\"" + this.name + "\":" + "\"" + trim_quote(this.value) + "\",";
		}
	});
	str = str.replace(/^\s+|\s+$/g, "") == "" ? "" : str.substring(0,str.length - 1);
	return $.parseJSON("{" + str + "}");
}
/**
 * 
 * 将form中的列表成数组,本方法依赖于jquery.js
 * 
 * @param formId 表单的Id
 * @param elementId表单中每组数据的Id模糊匹配
 * 
 * @return 
 * 
 */
function form2array(formId,elementId) {
	var array = new Array();
	$("#"+formId).find("[id^="+elementId+"]").each(function(i,n){
		array.push(form2json($(n).attr('id')));
	})
	return array;
}

/**
 * 将form中的不一致元素变为红色
 * 
 * @param id
 */
function form2red(id) {
	var formatInfo = form2json(id);
	var redKeys = [];
	var blackKeys = [];
	for ( var formatInfoKey in formatInfo) {
		var formatInfoKeyOld = "old"
				+ formatInfoKey.substring(0, 1).toUpperCase()
				+ formatInfoKey.substring(1)
		if (formatInfo[formatInfoKeyOld] !== undefined) {
			if (formatInfo[formatInfoKey] !== formatInfo[formatInfoKeyOld]) {
				redKeys.push(formatInfoKey);
			} else {
				blackKeys.push(formatInfoKey);
			}
		}
	}
	// 单行文本
	$("#" + id).find("input").filter(":text").each(function() {
		var redTmp = this;
		if (redTmp.name != null && redTmp.name != "") {
			$.each(redKeys, function(i, n) {
				if (n === redTmp.name) {
					$(redTmp).css({
						color : "#ff0011"
					});
					return false;
				}
			});
			$.each(blackKeys, function(i, n) {
				if (n === redTmp.name) {
					$(redTmp).css({
						color : "#000000"
					});
					return false;
				}
			});
		}
	});

	// 单行密码
	$("#" + id).find("input").filter(":password").each(function() {
		var redTmp = this;
		if (redTmp.name != null && redTmp.name != "") {
			$.each(redKeys, function(i, n) {
				if (n === redTmp.name) {
					$(redTmp).css({
						color : "#ff0011"
					});
					return false;
				}
			});
			$.each(blackKeys, function(i, n) {
				if (n === redTmp.name) {
					$(redTmp).css({
						color : "#000000"
					});
					return false;
				}
			});
		}
	});

	// 隐藏
	$("#" + id + " input[type='hidden']").each(function() {
		var redTmp = this;
		if (redTmp.name != null && redTmp.name != "") {
			$.each(redKeys, function(i, n) {
				if (n === redTmp.name) {
					$(redTmp).css({
						color : "#ff0011"
					});
					return false;
				}
			});
			$.each(blackKeys, function(i, n) {
				if (n === redTmp.name) {
					$(redTmp).css({
						color : "#000000"
					});
					return false;
				}
			});
		}
	});

	// 下拉列表
	$("#" + id).find("select").each(function() {
		var redTmp = this;
		if (redTmp.name != null && redTmp.name != "") {
			$.each(redKeys, function(i, n) {
				if (n === redTmp.name) {
					$(redTmp).css({
						color : "#ff0011"
					});
					return false;
				}
			});
			$.each(blackKeys, function(i, n) {
				if (n === redTmp.name) {
					$(redTmp).css({
						color : "#000000"
					});
					return false;
				}
			});
		}
	});

	// 单选框
	$("#" + id).find("input").filter(":radio").filter(":checked").each(
			function() {
				var redTmp = this;
				if (redTmp.name != null && redTmp.name != "") {
					$.each(redKeys, function(i, n) {
						if (n === redTmp.name) {
							$(redTmp).css({
								color : "#ff0011"
							});
							return false;
						}
					});
					$.each(blackKeys, function(i, n) {
						if (n === redTmp.name) {
							$(redTmp).css({
								color : "#000000"
							});
							return false;
						}
					});
				}
			});

	// 复选框
	$("#" + id).find("input").filter(":checkbox").filter(":checked").each(
			function() {
				var redTmp = this;
				if (redTmp.name != null && redTmp.name != "") {
					$.each(redKeys, function(i, n) {
						if (n === redTmp.name) {
							$(redTmp).css({
								color : "#ff0011"
							});
							return false;
						}
					});
					$.each(blackKeys, function(i, n) {
						if (n === redTmp.name) {
							$(redTmp).css({
								color : "#000000"
							});
							return false;
						}
					});
				}
			});

	// 多行文本
	$("#" + id).find("textarea").each(function() {
		var redTmp = this;
		if (redTmp.name != null && redTmp.name != "") {
			$.each(redKeys, function(i, n) {
				if (n === redTmp.name) {
					$(redTmp).css({
						color : "#ff0011"
					});
					return false;
				}
			});
			$.each(blackKeys, function(i, n) {
				if (n === redTmp.name) {
					$(redTmp).css({
						color : "#000000"
					});
					return false;
				}
			});
		}
	});
}

/**
 * 将form中的不一致元素变为红色
 * 
 * @param id
 */
function formlist2red(id) {
	$.each($('div[id^=' + id + ']'), function(i, n) {
		form2red($(n).attr('id'));
	});
}
/**
 * 检查form中的新老项目是否完全一致
 * @param id
 */
function checkIsSame(id){
	var flag = false;
	$.each($('div[id^=' + id + ']'), function(i, n) {
		var formatInfo = form2json(n.id);
		var notSameKeys = [];
		for ( var formatInfoKey in formatInfo) {
			var formatInfoKeyOld = "old" + formatInfoKey.substring(0, 1).toUpperCase() + formatInfoKey.substring(1)
			if (formatInfo[formatInfoKeyOld] !== undefined) {
				if (formatInfo[formatInfoKey] !== formatInfo[formatInfoKeyOld]) {
					notSameKeys.push(formatInfoKey);
				}
			}
		}
		if(notSameKeys.length == 0){
			flag = true;
		}
	});
	return flag;
} 
/**
 * 转译
 * 
 * @param {}
 *            str
 * @return {}
 */
function trim_quote(str) {
	return trim(str).replace(/\\/g, '\\\\').replace(/\"/g, '\\\"').replace(
			/\r/g, '\\r').replace(/\n/g, '\\n').replace(/\t/g, '\\t').replace(
			/\f/g, '\\f');
}
/**
 * 删除左空格
 * 
 * @param arg
 * @return string
 * @since 1.0
 * @author wangl
 */
function ltrim(arg) {
	return arg.replace(/^\s*/, "");
}

/**
 * 删除右空格
 * 
 * @param name
 * @return string
 * @since 1.0
 * @author wangl
 */
function rtrim(s) {
	return s.replace(/\s*$/, "");
}

/**
 * 删除左右空格
 * 
 * @param name
 * @return string
 * @since 1.0
 * @author wangl
 */
function trim(s) {
	return rtrim(ltrim(s));
}